﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ECommerce.DAL;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for TblTaxExtended
/// </summary>
namespace Entity
{
    public partial class TblTax
    {
        public static TblTax GetTax(String city, String state)
        {
            double maxtax = 0;
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@City", SqlDbType.VarChar, 0, ParameterDirection.Input, city);
            AddParameterToSqlCmd(sqlCmd, "@State", SqlDbType.VarChar, 0, ParameterDirection.Input, state);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblTax_GetTblTaxByCityState");
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblTaxCollectionFromReader);
            CustomCollection<TblTax> objCollection = ((CustomCollection<TblTax>)ExecuteReaderCmd(sqlCmd, test));
            TblTax taxCalculated=null;
            if (objCollection != null && objCollection.Count > 0)
            {
                if (objCollection.Count > 1)
                {
                    for (int index = 0; index < objCollection.Count; index++)
                    {
                        if (Convert.ToDouble(objCollection[index].TAX_RATE_TOTAL) > maxtax)
                        {
                            taxCalculated = objCollection[index];
                        }
                    }
                    
                }
                else
                {
                    taxCalculated = objCollection[0];                    
                }
            }
            return taxCalculated;
        }

        public static CustomCollection<TblTax> GetAllTaxByStateNamePaged(string stateName, string orderBy, int startRow, int pageSize)
        {
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@stateName", SqlDbType.VarChar, 50, ParameterDirection.Input, stateName);
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startrow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddParameterToSqlCmd(sqlCmd, "@pagesize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "sp_TblTax_Get_All_Tax_By_StateName_Paged");
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblTaxCollectionFromReader);
            CustomCollection<TblTax> objCollection = ((CustomCollection<TblTax>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;

        }
        public static int GetAllTaxByStateNameCount(string stateName)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@stateName", SqlDbType.VarChar, 50, ParameterDirection.Input, stateName);
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "sp_TblTax_Get_All_Tax_By_StateName_Count");
            ExecuteScalarCmd(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

    }
}